Witcher 3 remaster HDR fix - source code
========================================

This folder shows exactly what the mod changes. It is not needed to play.

The mod replaces 4 of the game's own shaders. The game's own HDR does everything else.
We did not rewrite these shaders. We edited the game's compiled shaders directly, at the
level Microsoft's shader tool shows when it disassembles a shader (DXIL / LLVM IR).
The "modified" files below are the code the mod's shaders were built from.

Files
-----
addon.cpp
    The RenoDX add-on: which 4 shaders are replaced, the buttons and sliders,
    the F8 switch, and the 4 values sent to the shaders (constant buffer b13, space 50):
      x = option (0 = game, 8 = Game HDR +, 5 = RVT A, 9 = RVT B)
      y = Game HDR +: full brightness at least at (x SDR white)
      z = Game HDR +: boost start (fraction of SDR white)
      w = black level fix strength (0..1)

Shaders\<name> - 1 original game shader.ll   the game's shader, disassembled
Shaders\<name> - 2 modified by the mod.ll    the same shader with our changes
Shaders\<name> - 3 changes only.diff         only the lines we added (+) or replaced (-)

Start with the "changes only" files. Every value we added has a name starting with %k,
so our code is easy to find in the modified files too.

The 4 shaders
-------------
tonemap_0x382CDBDB and tonemap2_0x724E225F  (the game's tone curve)
    - Black level fix: the game's curve lifts black above zero. We calculate the curve's
      own output for black from the curve's settings (%kb), run it through the game's own
      code that follows the curve (%kc..., a copy of the game's code), and remove it in
      linear light (%kd...):  out = max(x - k*b, 0) / (1 - k*b), k = slider strength (%kfix).
    - Test pixels: the top-left 15 pixels get known values 2^(i-5) (%kprobe, %kv) before the
      curve, so the HDR output shader can see what the curve, colour table and grading do
      to them.

glow_0xFB1B4062  (glow pass)
    - Leaves the 15 test pixels untouched (%kprobe).

hdrout_0x8F5737B5  (the game's HDR output: paper white, max nits, highlight boost, PQ)
    - Reads the 15 test pixels (%kA, %kPA, %kPY) and finds where the current scene's
      SDR white and the top of the curve are (%kcw, %kcs, %kc...).
    - Game HDR + (option 8): paper white as labelled - the game's hidden x1.5 is removed
      (%kpwl, %kpw); boost start and end placed from the scene's SDR white (%kthr, %kend);
      brightest highlights reach the max nits setting.
    - RVT A / RVT B (options 5 and 9): the remaining %k code near the end.
    - Paints the 15 test pixels over with the pixel below, so they are not visible.

What a developer would do instead
---------------------------------
The test pixels only exist because a mod cannot read the engine's settings.
The engine already knows these values (the scene's SDR white, the curve's settings)
and can pass them to the HDR output shader directly. The rest of the maths stays the same.

License: MIT (see LICENSE.txt in the mod download). Built on the RenoDX framework by ShortFuse.
